package com.bw.car_module.carlist.bind;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.car_module.carlist.CartAdapter;
import com.bw.car_module.carlist.CartValueEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.List;

public class CartBind {

    @BindingAdapter(value = {"cartList","cartChildClick",
            "cartItemClick"})
    public static void bindCart(RecyclerView recyclerView,
                                List<CartValueEntity>cartList,
                                OnItemChildClickListener cartChildClick,
                                OnItemClickListener cartItemClick){
        CartAdapter cartAdapter = (CartAdapter) recyclerView.getAdapter();
        if (cartAdapter == null){
            cartAdapter = new CartAdapter();
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            cartAdapter.setOnItemChildClickListener(cartChildClick);
            cartAdapter.setOnItemClickListener(cartItemClick);
            recyclerView.setAdapter(cartAdapter);
        }else {
            cartAdapter.setNewInstance(cartList);
        }
    }

}
